﻿//---------------------------------------------------------------------
//  Copyright (c) 2012-2013 Daun Project.  All rights reserved.
//  http://daun.codeplex.com
//  http://www.daun-project.com
//---------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;

using Daun.Cores;
using Daun.Cores.Entities;
namespace Daun.Forms
{
    public partial class ConnectForm : Form
    {
        private MongoEngine _engine = null;
        MongoServerCollection _config = null;
        public MongoEngine DBEngine
        {
            get
            {
                return _engine;
            }
        }
        public ConnectForm()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            int port = 27017;
            string error = string.Empty;
            if(!string.IsNullOrEmpty(txtPort.Text))
                port = Convert.ToInt32(txtPort.Text);

            bool success = MongoEngine.TestConnection(txtHost.Text,
                                            port, txtPort.Text, 
                                            txtPassword.Text, 
                                            txtDatabase.Text,ref error);
            if (success)
                MessageBox.Show("Successfully connected to MongoDB server", 
                    "Information", 
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show(error,
                    "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            int port = 27017;
            string error = string.Empty;
            if (!string.IsNullOrEmpty(txtPort.Text))
                port = Convert.ToInt32(txtPort.Text);

            _engine = MongoEngine.Connect(txtHost.Text,
                                            port, txtUserName.Text,
                                            txtPassword.Text, txtDatabase.Text, ref error);
            if (_engine != null)
            {
                if (_config.Configs != null)
                    _config.Configs.Remove(txtHost.Text.ToLower());

                MongoServer config = new MongoServer();
                config.ServerName = txtHost.Text;
                config.Port = 27017;
                config.Database = txtDatabase.Text;
                if (!string.IsNullOrEmpty(txtPort.Text))
                    config.Port = Convert.ToInt32(txtPort.Text);
                config.UserName = txtUserName.Text;
                config.Password = "";
                if (chckSavePassword.Checked)
                    config.Password = txtPassword.Text;
                _config.Configs.Add(txtHost.Text.ToLower(), config);
                MongoHelpers.SaveConfiguration(_config);

                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
            else
                MessageBox.Show(error,
                    "Information",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void txtPort_KeyPress(object sender, KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
        }

        private void ConnectForm_Load(object sender, EventArgs e)
        {
            LoadConfig();
        }

        private void LoadConfig()
        {
            _config = MongoHelpers.LoadConfiguration();
            if (_config != null)
            {
                if (_config.Configs != null)
                {
                    foreach (KeyValuePair<string, MongoServer> item in _config.Configs)
                        cboConfig.Items.Add(item.Key);
                }
                else
                    _config.Configs = new Dictionary<string, MongoServer>();

            }
            else
            {
                _config = new MongoServerCollection();
                _config.Configs = new Dictionary<string, MongoServer>();                
            }
            _config.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        private void cboConfig_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_config.Configs != null)
            {
                if (_config.Configs != null)
                {
                    MongoServer config = _config.Configs[cboConfig.SelectedItem.ToString()];
                    txtHost.Text = config.ServerName;
                    txtPort.Text = config.Port.ToString();
                    txtUserName.Text = config.UserName;
                    txtPassword.Text = config.Password;
                    txtDatabase.Text = config.Database;
                }
            }
        }
    }
}
